package com.zhny.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author create by Alex on 4/6/2018
 */
@Component
public class JsonBuilder {

    //TODO:前台逻辑看不懂，暂时先写死，对于不同地区IJsonService的实体类不同，简单工厂模式
    @Autowired
    private IJsonService jsonService;

    @Autowired
    private ObjectMapper mapper;

    public String buildJsonFromService(Integer deviceFlag) throws JsonProcessingException {
        return buildJson(jsonService.selectDataByDeviceFlag(deviceFlag));
    }

    public String buildJson(Object object) throws JsonProcessingException {
        return mapper.writeValueAsString(object);
    }

    public IJsonService getJsonService() {
        return jsonService;
    }

    public void setJsonService(IJsonService jsonService) {
        this.jsonService = jsonService;
    }

    public ObjectMapper getMapper() {
        return mapper;
    }

    public void setMapper(ObjectMapper mapper) {
        this.mapper = mapper;
    }
}
